﻿Imports System.Collections.ObjectModel
Imports System.Windows.Controls
Imports System.Windows.Media

Public Class VisualCode

    Private Sub _main_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        If DataContext IsNot Nothing Then
            GenerateVisualCode()
            CalculateItemWidth()
        End If
    End Sub

    Private Sub GenerateVisualCode()
        Try
            Dim c As Collection(Of BarCodeStroke)
            Dim context As IBarCode = DataContext
            c = context.BinaryCode
            _strokecontainer.Children.Clear()
            For Each item As BarCodeStroke In c
                Dim g As New Grid()
                g.Background = New SolidColorBrush(item.Color)
                _strokecontainer.Children.Add(g)
            Next
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub CalculateItemWidth()
        Try
            Dim elements As Integer = _strokecontainer.Children.Count
            If elements <> 0 Then
                Dim actualwidth As Double = _strokecontainer.ActualWidth
                _strokecontainer.ItemWidth = Math.Floor(actualwidth / elements)
            End If
        Catch ex As Exception

        End Try
       
    End Sub

    Private Sub _main_DataContextChanged(sender As System.Object, e As System.Windows.DependencyPropertyChangedEventArgs) Handles MyBase.DataContextChanged
        If DataContext IsNot Nothing Then
            GenerateVisualCode()
            CalculateItemWidth()
        End If
    End Sub
End Class
